package model;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class DbHelper extends IPresenter {
	
	public DbHelper() {
		super();
		
	}

	
	public String[] getLocations() {
		String sql = "SELECT DISTINCT " 
				+ DbSchema.LOC_NAME
				+ " FROM " + DbSchema.LOC_TABLE;
		ResultSet rs = executeSQLQuery(sql);
		
		String[] out = null;
		ArrayList<String> strings = new ArrayList<String>();
		
		try {
			rs.first();
			
			while(rs.next()) {
				String location = rs.getString(DbSchema.LOC_NAME);
				strings.add(location);
			}
			
			int count = strings.size();
			out = new String[count];
			
			for(int i = 0; i < count; i++) {
				out[i] = strings.get(i);
			}
			
		}
		catch (SQLException e){
			e.printStackTrace();
		}
		return out;
	}
	
	public String[] getCarTypes() {
		String sql = "SELECT DISTINCT " 
				+ DbSchema.CAR_TYPE
				+ " FROM " + DbSchema.CAR_TABLE;
		ResultSet rs = executeSQLQuery(sql);
		
		String[] out = null;
		ArrayList<String> strings = new ArrayList<String>();
		
		try {
			rs.first();
			
			while(rs.next()) {
				String location = rs.getString(DbSchema.CAR_TYPE);
				strings.add(location);
			}
			
			int count = strings.size();
			out = new String[count];
			
			for(int i = 0; i < count; i++) {
				out[i] = strings.get(i);
			}
			
		}
		catch (SQLException e){
			e.printStackTrace();
		}
		return out;
	}
	
	public String[] getTransmissionType() {
		return new String[] {"Manual", "Automatic"};
	}
	
	public String[] getCars(String location) {
		String[] out = new String[1];
		if(checkString2(location)) {
			String sql = "SELECT DISTINCT " + DbSchema.CAR_MODEL + " FROM " + DbSchema.CAR_TABLE + " WHERE LOC = '" + location + "';";
			ResultSet rs = executeSQLQuery(sql);
			
			out = null;
			ArrayList<String> strings = new ArrayList<String>();
			
			try {
				rs.first();
				
				while(rs.next()) {
					String model = rs.getString(DbSchema.CAR_MODEL);
					System.out.println("Model is: " + model);
					strings.add(model);
				}
				
				int count = strings.size();
				out = new String[count];
				
				for(int i = 0; i < count; i++) {
					out[i] = strings.get(i);
				}
				
			}
			catch (SQLException e){
				e.printStackTrace();
			}
		}
		return out;
	}
	
	public String[] getCarModels() {
		String sql = "SELECT DISTINCT " + DbSchema.CAR_MODEL + " FROM " + DbSchema.CAR_TABLE + ";";
		ResultSet rs = executeSQLQuery(sql);
		
		String[] out = null;
		ArrayList<String> strings = new ArrayList<String>();
		
		try {
			rs.first();
			
			while(rs.next()) {
				String model = rs.getString(DbSchema.CAR_MODEL);
				strings.add(model);
			}
			
			int count = strings.size();
			out = new String[count];
			
			for(int i = 0; i < count; i++) {
				out[i] = strings.get(i);
			}
			
		}
		catch (SQLException e){
			e.printStackTrace();
		}
		
		return out;
	}
	
	public String[] getUserTypes() {
		return new String[] {"EMPLOYEE", "MEMBER", "ADMINISTRATOR"};
	}


	public String[] getBluetooth() {
		return new String[] {"Y", "N"};
	}


	public String[] getAuxCable() {
		return new String[] {"Y", "N"};
	}
}
